
import 'package:flutter_app/bean/girl_data.dart';
import 'package:flutter_app/compents/state_page.dart';
import 'package:flutter_app/controller/base/base_getx_controller_with_refresh.dart';
import 'package:flutter_app/http/http_manager.dart';
import 'package:flutter_app/http/request_api.dart';
import 'package:get/get.dart';
class GirlController extends BaseGetXControllerWithRefesh {
  var _girls = <Girl>[].obs;
  var isList = false.obs;
  List<Girl> get girls => _girls;
  int _pageIndex = 1;

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    initData(true);
  }

  void getGirls(bool isShowLoading, {bool refresh}) {
    handleRequest(
        HttpManager.instance.get("",
            newUrl: RequestApi.QUERY_GIRLS_API,
            params: {"page": _pageIndex},
            refresh: refresh,
            list: true),
        isShowLoading,
      (value) {
      //返回成功结果
      var result = GirlData.fromJson(value).data;
      //当前页码
      var curPage = result.page;
      //总页数
      var pageCount = result.totalCount;
      if (_pageIndex == 1) {
        _girls.clear();
      }
      //妹子数据
      _girls.addAll(result.list);
      if (curPage == 1 && result.list.length == 0) {
        loadState.value = LoadState.EMPTY;
      } else if (curPage == pageCount) {
        loadState.value = LoadState.NO_MORE;
        refreshController.loadNoData();
        refreshController.refreshCompleted(resetFooterState: true);
      } else {
        loadState.value = LoadState.SUCCESS;
        refreshController.loadComplete();
        refreshController.refreshCompleted(resetFooterState: true);
        _pageIndex++;
      }
    },
    failure: (error) {
      refreshController.loadFailed();
      refreshController.refreshFailed();
    });
  }

  @override
  void initData(bool isShowLoading) {
    getGirls(isShowLoading);
  }

  @override
  void refresh() {
    _pageIndex = 1;
    getGirls(false, refresh: true);
  }
}
